#include "tm1638.h"

u8 DispTab[4];
u8 Update_disp_flg=0;                   //更新显示标志
static uint8_t keytime = 0;             //按键扫描时间
KEY_TypeDef KeyValue = {0};              //保存按键值
LED_TypeDef LedValue = {0};              //保存led灯

 //TM1638_keytime_handle  扫描按键
void TM1638_keytime_handle(void)  
{
    keytime ++;   //定时扫描按键
    
    if(keydis.PressFlg == true)
    {
        //按键按下计时
        keydis.cnt++;
    }
} 

void TM1638_Write(u8 DATA)              //写数据函数
{
   u8 i;
   taskENTER_CRITICAL();                //进入临界区
   TM1638_DIO_OUT;
   for(i=0;i<8;i++)
   {
     TM1638_CLK=0;
     delay_us(1);
     if(DATA&0X01)
     {
       TM1638_DIO_DataOUT=1;
     }
     else
     {
       TM1638_DIO_DataOUT=0;
     }
     DATA>>=1;
     TM1638_CLK=1;
     delay_us(1);
   }
     taskEXIT_CRITICAL();            //退出临界区
}

u8 TM1638_Read(void)                //读数据函数
{
  u8 i;
  u8 temp=0;
    taskENTER_CRITICAL();           //进入临界区
  TM1638_DIO_IN;
  for(i=0;i<8;i++)
  {
    temp>>=1;
    TM1638_CLK=0;
    delay_us(1);
    if(TM1638_DIO_DataIN==1)
    {
      temp|=0x80;
    }
    TM1638_CLK=1;
    delay_us(1);
  }
    taskEXIT_CRITICAL();            //退出临界区
  return temp;
}

void Write_COM(u8 cmd) //发送命令字
{
  TM1638_STB=0;
  TM1638_Write(cmd);
  TM1638_STB=1;
}

void Read_key(u8 *KeyValue)
{
     u8 i;
     TM1638_STB=0;
     TM1638_Write(0x42);           //读键扫数据 命令 
     delay_us(10);
     for(i=0;i<4;i++) 
         KeyValue[i]=TM1638_Read();//4个字节数据合成一个字节 
     TM1638_STB=1;           
}

void Write_DATA(u8 add,u8 DATA) //指定地址写入数据
{
     Write_COM(0x44);      //固定地址
     TM1638_STB=0;
     TM1638_Write(0xc0|add);
     TM1638_Write(DATA);
     TM1638_STB=1;
}

//数据保存到显示缓冲区
void display(u8 dig,u8 dat)
{
  DispTab[dig]=dat;
}

//显示LED
void display_led(u8 dig,u8 dat)
{
  Write_DATA(dig,dat);
}

u8 Temp[15];
u8 DigitalTube[9]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
//dig数码管位，seg数码管段，onoff数码管亮灭

void Digital_led(u8 dig,u8 seg,u8 onoff)
{
  if(onoff)Temp[dig]|=DigitalTube[seg];
  else     Temp[dig]&=(~DigitalTube[seg]);
  Write_DATA((0xc0|dig),Temp[dig]);
}

void DigitalTube_led(u8 dig,u8 seg,u8 onoff)
{
  if(onoff)Temp[dig]|=DigitalTube[seg];
  else     Temp[dig]&=(~DigitalTube[seg]);
  Write_DATA((0xc0|dig*2),Temp[dig]);
}

void TM1638_Digital_LED_SET(uint8_t GRID, uint16_t led_value)
{
  //地址命令设置
  Write_DATA( (GRID+0),led_value);
  Write_DATA( (GRID+1),led_value>>8 );
}

//闪烁
void Flicker(uint8_t n)
{
  u8 i;
  for(i=0;i<n;i++)
  {
    display_led(TM1638_GRID1+i*2,0x00);
  }
}

//清除数码管显示
void dispclear(u8 n,u8 m)
{
  u8 i;
  if(m)
  {
     for(i=0;i<n;i++)
     {
         DispTab[i]=0x00;
         display_led(TM1638_GRID1+2*i+1,DispTab[i]);
     }
  }
  else
  {
     n = 4 - n;
     for (i=4; i>n; i--)
     {
         DispTab[i-1]=0x00;
         display_led(TM1638_GRID1+2*i+1,DispTab[i]);
     }        
  }
}

void UpdateDisp(void)    //更新四段数码管显示
{
  u8 i;
  for(i=0;i<4;i++)
  {
    display_led(TM1638_GRID1+i*2,DispTab[i]);
  }   
}


 //TM1638初始化函数
void TM1638_Init(void)
{
  Write_COM(0x8a);       //亮度 (0x88-0x8f)8级亮度可调
  Write_COM(0x40);       //采用地址自动加1
  TM1638_STB=0;          
  TM1638_Write(0xc0);    //设置起始地址
  for(u8 i=0;i<16;i++)      //传送16个字节的数据
  {
    TM1638_Write(0x00);
  }
  TM1638_STB=1;
  
//  Write_DATA(0xc8,0x55);
//  Write_DATA(0xc8,0xAA);  
//  Write_DATA(0xc9,0x01);
//  Write_DATA(0xc9,0x02);  
//  Write_DATA(0xca,0x55);
//  Write_DATA(0xca,0xAA);  
//  Write_DATA(0xcb,0x01);
//  Write_DATA(0xcb,0x02);   
  
  //清除缓存
  memset(&LedValue,0,sizeof(LedValue));
  memset(&KeyValue,0,sizeof(KeyValue));
}

#if PT100_TEMP_EN
void led_Temp_alarm_show(uint8_t num,uint8_t state)
{
  switch(num)
  {
    case 0:
      if( state == 0)
      {
        LedValue.bit.alarm_blue_1 = 1;//正常
        LedValue.bit.alarm_red_1 = 0; 
      }else{
        LedValue.bit.alarm_red_1 = 1; //异常
        LedValue.bit.alarm_blue_1 = 0;
      }
      break;
    case 1:
      if( state == 0)
      {
        LedValue.bit.alarm_blue_2 = 1;//正常
        LedValue.bit.alarm_red_2 = 0; 
      }else{
        LedValue.bit.alarm_red_2 = 1; //异常
        LedValue.bit.alarm_blue_2 = 0;
      }
      break;
    case 2:
      if( state == 0)
      {
        LedValue.bit.alarm_blue_3 = 1;//正常
        LedValue.bit.alarm_red_3 = 0; 
      }else{
        LedValue.bit.alarm_red_3 = 1; //异常
        LedValue.bit.alarm_blue_3 = 0;
      }
      break;
    case 3:
      if( state == 0)
      {
        LedValue.bit.alarm_blue_4 = 1;//正常
        LedValue.bit.alarm_red_4 = 0; 
      }else{
        LedValue.bit.alarm_red_4 = 1; //异常
        LedValue.bit.alarm_blue_4 = 0;
      }
      break;
  }
}
#endif

//led_handle led灯事件
void led_handle(void)
{
    static uint8_t run_time = 0;

    LedValue.uled1.bit.Power_RedLed = 1;      //电源灯
  
    LedValue.uled1.bit.Net_RedLed = NETDET;    //网络灯
    LedValue.uled1.bit.Net_blueLed = !NETDET;    //网络灯
    
    #if Air724_EN
    LedValue.uled1.bit.WLAN_RedLed = !Air724_CMD.Online;    //WLAN无线网络状态灯
    LedValue.uled1.bit.WLAN_blueLed = Air724_CMD.Online;    //WLAN无线网络状态灯
    #else
    LedValue.uled1.bit.WLAN_RedLed = 0;    //WLAN无线网络状态灯
    LedValue.uled1.bit.WLAN_blueLed = 0;    //WLAN无线网络状态灯
    #endif
  
    #if BT_TASK_EN
    LedValue.uled1.bit.Blue_RedLed = 0;     //蓝牙状态灯
    LedValue.uled1.bit.Blue_blueLed = 1;    //蓝牙状态灯
    #else
    LedValue.uled1.bit.Blue_RedLed = 0;     //蓝牙状态灯
    LedValue.uled1.bit.Blue_blueLed = 0;    //蓝牙状态灯
    #endif
  
    
    if( run_time == 0 ){
       LedValue.uled1.bit.Run_blueLed = 1;     //运行灯
       run_time = 4; 
    }else{
       run_time --;
       if( run_time < 2 ){
          LedValue.uled1.bit.Run_blueLed = 0;  //运行灯
       }            
    }
   
//     //显示led灯
//    static KEY_TypeDef led_temp = {0};
//    static uint8_t cnt = 0;
//    led_temp.HEX_32 = 0x01<<((cnt++)%32);
//    TM1638_Digital_LED_SET(TM1638_GRID9,led_temp.HEX_8,4 );   //测试

    TM1638_Digital_LED_SET(TM1638_GRID15,LedValue.uled1.HEX_16); //GRID8 硬件引脚
    TM1638_Digital_LED_SET(TM1638_GRID13,LedValue.uled2.HEX_16); //GRID7 硬件引脚
    TM1638_Digital_LED_SET(TM1638_GRID9 ,LedValue.uled3.HEX_16); //GRID5 硬件引脚
    TM1638_Digital_LED_SET(TM1638_GRID11,LedValue.uled4.HEX_16); //GRID6 硬件引脚
}

//tm1638按键的读取
void key_handle(void)
{
    Read_key(KeyValue.HEX_8);
  
    if( KeyValue.HEX_32 )
    {
        //按键  ---》》》》按下
        if( KeyValue.HEX_32 == keydis.KeyVal)
        {
            keydis.PressStatus = true;                  //按下状态
            keydis.PressFlg = true;                     //按下标志位
            if ( keydis.cnt > LONG_TIME)
            {
                keydis.ProEvent = true;
                keydis.KeyStatus = LONG_ENTER;          //长按状态
            }
            else
            {
               keydis.KeyStatus = SHORT_ENTER;          //短按状态
               keydis.ProEvent = true;                 //还没处理,置按键处理标记
            }
        }            
        else{            
            keydis.KeyVal = KeyValue.HEX_32;
        }
    }else{
          //按键  ---》》》》弹起
         if (keydis.PressFlg == true)
         {
            if (keydis.cnt>=LONG_TIME)
            {
               keydis.KeyStatus = NONE_ENTER;
               keydis.ProEvent = false;                 //已经处理完一次
            }
            else
            {
               keydis.KeyStatus = SHORT_ENTER;          //短按状态
               keydis.ProEvent = true;                 //还没处理,置按键处理标记
            }
            keydis.PressStatus = false;               //弹起
         }
         else
         {
            keydis.KeyStatus = NONE_ENTER;
            keydis.FirPre = true;
            keydis.ProEvent = false;
         }
         keydis.PressFlg = false;
         keydis.cnt = 0;      
    }
}

